﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.DataAnnotations;

namespace VehicleMaintenance.UserControls
{
    public partial class ucQueryList : UserControl
    {
        public ucQueryList()
        {
            InitializeComponent();

            dgvData.AutoGenerateColumns = false;
        }

        /// <summary>
        /// 初始化列头
        /// </summary>
        public void InitializeColumnHeader(Type t)
        {

            dgvData.Columns.Clear();

            foreach (System.Reflection.PropertyInfo p in t.GetProperties())
            {
                DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
                col.Name = p.Name;
                col.ReadOnly = true;
                col.DataPropertyName = p.Name;

                var a = p.GetCustomAttributes(typeof(DisplayAttribute), true);
                if (a.Count() > 0)
                {
                    DisplayAttribute display = a.Single() as DisplayAttribute;
                    col.HeaderText = display.Name;
                }
                else
                {
                    col.Visible = false;
                }
                switch (p.PropertyType.Name.ToLower())
                {
                    case "decimal":
                        {
                            DataGridViewCellStyle s = new DataGridViewCellStyle(); 
							s.Alignment = DataGridViewContentAlignment.MiddleRight;
                            col.DefaultCellStyle = s;
                            break;
                        }
                }
                dgvData.Columns.Add(col);
            }
        }

        /// <summary>
        /// 绑定的数据源
        /// </summary>
        public object DataSource
        {
            get
            {
                return dgvData.DataSource;
            }
            set
            {
                dgvData.DataSource = value;
            }
        }
    }
}
